package com.example.demo.properties.project;

import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * 软件著作权:王艳明
 * 系统名称:SpringBoot后台管理系统
 *
 * @author WangYanming
 * @ClassName 类:ProjectModel.java
 * @Description 功能描述:projectModel
 * @Version 1.0
 * @date 2025-10-13 上午 10:16
 */
@Data
public class ProjectModel {

    private String name;

    private List<ProjectDependencyModel> dependencies;

    private ProjectSettingsModel settings;

    public static ProjectModel mapToObj(Map<String, Object> map) {
        ProjectModel projectModel = new ProjectModel();
        // name
        String name = (String) map.get("name");
        projectModel.setName(name);
        // dependencies
        List<ProjectDependencyModel> dependencies = Lists.newArrayList();
        Map<String, Map<String, String>> dependenciesMap = (Map<String, Map<String, String>>) map.get("dependencies");
        for (Map.Entry<String, Map<String, String>> entry : dependenciesMap.entrySet()) {
            ProjectDependencyModel projectDependencyModel = new ProjectDependencyModel();
            // 遍历内map
            Map<String, String> dependencyMap = entry.getValue();
            for (Map.Entry<String, String> entryInner : dependencyMap.entrySet()) {
                String key = entryInner.getKey();
                System.out.println("key:" + key);
                String value = entryInner.getValue();
                System.out.println("value:" + value);
                if ("name".equals(key)) {
                    projectDependencyModel.setName(value);
                } else if ("version".equals(key)) {
                    projectDependencyModel.setVersion(value);
                }
            }
            dependencies.add(projectDependencyModel);
        }
        // jdk8+
        System.out.println("dependenciesMap.entrySet().forEach(entry -> {");
        dependenciesMap.entrySet().forEach(entry -> {
            System.out.println("entry.getKey():" + entry.getKey());
            System.out.println("entry.getValue():" + entry.getValue());
            System.out.println(",");
        });
        System.out.println("});");
        // jdk8+
        System.out.println("dependenciesMap.forEach((key, value) -> {");
        // 使用如下Map<String, ProjectDependencyModel> dependenciesMap = (Map<String, ProjectDependencyModel>) map.get("dependencies");
        // forEach会报错,需要改为Map<String, Map<String, String>> dependenciesMap = (Map<String, , Map<String, String>>) map.get("dependencies");
        // 进一步处理否则Map<String, String>不会直接转成ProjectDependencyModel报错
        dependenciesMap.forEach((key, value) -> {
            System.out.println("key:" + key);
            System.out.println("value:" + value);
            System.out.println(",");
        });
        System.out.println("});");
        projectModel.setDependencies(dependencies);
        // settings
        Map<String, Object> settingsObj = (Map<String, Object>) map.get("settings");
        ProjectSettingsModel settings = ProjectSettingsModel.mapToObj(settingsObj);
        projectModel.setSettings(settings);
        return projectModel;

    }

    // 示例
    /*

    {
      "name" : "YAML Demo",
      "dependencies" : {
        "0" : {
          "name" : "lodash",
          "version" : "4.17.0"
        },
        "1" : {
          "name" : "axios",
          "version" : "0.24.0"
        }
      },
      "settings" : {
        "debug" : false,
        "logs" : {
          "0" : {
            "type" : "file",
            "path" : "/var/logs"
          },
          "1" : {
            "type" : "console"
          }
        }
      }
    }

     */


}
